package com.tianxiabuyi.persist;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by yang on 2017/3/25.
 */
public class Where {
    boolean isNull = false;
    int intRange = 0;// 0:all 1:>=0 2:>0
    ArrayList<String> nullString = new ArrayList<String>();
    String Where = "";
    Integer counter = 0;
    HashMap<Integer, Object> params = new HashMap<Integer, Object>();
    boolean isWhereStart = true;

    public Where() {
        nullString.add("");
        nullString.add("%%");
    }

    public Where(String key, Object value) {
        this();
        genPrepCond("", key, value);
    }

    public Where(Where Where) {
        this();
        genWhereCond("", Where);
    }

    public Where(boolean isNull) {
        this.isNull = isNull;
    }

    public Where(String where) {
        this();
        genCond("", where);
    }

    public int getIntRange() {
        return intRange;
    }

    public void setIntRange(int intRange) {
        this.intRange = intRange;
    }

    public ArrayList<String> getNullString() {
        return nullString;
    }

    public void setNullString(ArrayList<String> nullString) {
        this.nullString = nullString;
    }

    public String getWhere() {
        return Where;
    }

    public void setWhere(String Where) {
        this.Where = Where;
    }

    public Integer getCounter() {
        return counter;
    }

    public void setCounter(Integer counter) {
        this.counter = counter;
    }

    public HashMap<Integer, Object> getParams() {
        return params;
    }

    public void setParams(HashMap<Integer, Object> params) {
        this.params = params;
    }

    public Where and(String and) {
        return genCond("and", and);
    }

    public Where or(String or) {
        return genCond("or", or);
    }

    Where genCond(String cond, String whereValue) {
        if (!isNull(whereValue)) {
            if (isWhereStart) {
                cond = "";
            }
            Where = Where + cond + " (" + whereValue + ") ";
            isWhereStart = false;
        }
        return this;
    }

    Where genPrepCond(String cond, String key, Object value) {
        if (isNull(value)) {
            value = "-999999999999999999999999999"; // 如果为null等设置默认值
        }

        counter++;
        params.put(counter, value);
        //return genCond(cond, key + "? ");
        return genCond(cond, key + "?" + counter + " ");
    }

    Where genWhereCond(String cond, Where Where) {
        genCond(cond, Where.getWhere());
        Object[] whereParams = Where.getParams().values().toArray();
        for (int i = 0; i < whereParams.length; i++) {
            counter++;
            params.put(counter, whereParams[i]);
        }
        return this;
    }

    public Where and(String key, Object value) {
        return genPrepCond("and", key, value);
    }

    public Where or(String key, Object value) {
        return genPrepCond("or", key, value);
    }

    public Where and(Where Where) {
        return genWhereCond("and", Where);
    }

    public Where or(Where Where) {
        return genWhereCond("or", Where);
    }

    boolean isNull(Object value) {
        if (isNull) {
            return false;
        }
        if (value == null) {
            return true;
        }
        if (value instanceof Integer) {
            return isIntNull((Integer) value);
        } else if (value instanceof Long) {
            return isLongNull((Long) value);
        } else if (value instanceof String) {
            return isStringNull((String) value);
        }
        return true;// throw new ServiceException ("Unknown type",2);
    }

    boolean isIntNull(Integer value) {
        if (isNull) {
            return false;
        }

        if (value == null) {
            return true;
        }
        if (intRange == 1) {
            if (value < 0) {
                return true;
            }
        }
        if (intRange == 2) {
            if (value <= 0) {
                return true;
            }
        }
        return false;
    }

    boolean isLongNull(Long value) {
        if (isNull) {
            return false;
        }

        if (value == null) {
            return true;
        }
        if (intRange == 1) {
            if (value < 0l) {
                return true;
            }
        }
        if (intRange == 2) {
            if (value <= 0l) {
                return true;
            }
        }
        return false;
    }

    boolean isStringNull(String value) {
        if (isNull) {
            return false;
        }

        if (value == null) {
            return true;
        }
        String v = value.trim();
        for (int i = 0; i < nullString.size(); i++) {
            if (nullString.get(i).trim().equals(v)) {
                return true;
            }
        }
        return false;
    }
}